package com.zsrt.devbase.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
	/**
	 * 将不规范的String日期转成规范的String日期
	 * @param strDate
	 * @return
	 */
	public static String formatStringDate(String strDate){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		try {
			Date getDate = sdf.parse(strDate);
			String formatDate = sdf.format(getDate);
			return formatDate;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 将格式为：2017-03-28 15:49:48的时间
	 * 转化为：3月28日 星期二 15:49
	 * @param strDate
	 * @return
	 */
	public static String getInterviewDate(String strDate){
		//获得星期几
		//String weekOfDate = DateUtil.getWeekOfDate(strDate);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf1 = new SimpleDateFormat("M月dd日 E HH:mm"); 
		//SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm"); 
		try {
			//将String的日期转为Date的日期
			Date getDate = sdf.parse(strDate);
			//x月xx日
			String format1 = sdf1.format(getDate);
			//处理时和分钟
			//String format2 = sdf2.format(getDate);
			String returnDate = format1;
			return returnDate;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * 获取系统时间 String 类型 （默认格式yyyy-MM-dd HH:mm:ss）
	 * 
	 * @return
	 */
	public static String getSystemTimeStr() {
		return getSystemTimeStr(ConstantDefine.YMD_HMS);
	}

	/**
	 * 获取系统时间 String 类型 （自定义格式）
	 * 
	 * @param format
	 * @return
	 */
	public static String getSystemTimeStr(String format) {
		return getFormatTime(format, new Date());
	}

	/**
	 * 获取自定义格式的指定时间
	 * 
	 * @param format
	 *            默认yyyy-MM-dd HH:mm:ss格式
	 * @param date
	 * @return
	 */
	public static String getFormatTime(String format, Date date) {
		if (date == null) {
			date = new Date();
		}
		if (StringUtil.isEmpty(format)) {
			format = ConstantDefine.YMD_HMS;
		}
		SimpleDateFormat f = new SimpleDateFormat(format);
		return f.format(date);
	}

	/**
	 * 将日期字符串转化为日期。失败返回null。
	 * 
	 * @param date
	 *            日期字符串
	 * @param parttern
	 *            日期格式
	 * @return 日期
	 */
	public static Date StringToDate(String date, String parttern) {
		Date myDate = null;
		if (date != null) {
			try {
				myDate = getDateFormat(parttern).parse(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return myDate;
	}

	/**
	 * 将日期转化为日期字符串。失败返回null。
	 * 
	 * @param date
	 *            日期
	 * @param parttern
	 *            日期格式
	 * @return 日期字符串
	 */
	public static String DateToString(Date date, String parttern) {
		String dateString = null;
		if (date != null) {
			try {
				dateString = getDateFormat(parttern).format(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return dateString;
	}

	/**
	 * 获取SimpleDateFormat
	 * 
	 * @param parttern
	 *            日期格式
	 * @return SimpleDateFormat对象
	 * @throws RuntimeException
	 *             异常：非法日期格式
	 */
	private static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
		return new SimpleDateFormat(parttern);
	}

	/**
	 * 计算两个日期间隔的天数
	 * @param beginDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(Date beginDate, Date endDate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.YMD);
		beginDate = sdf.parse(sdf.format(beginDate));
		endDate = sdf.parse(sdf.format(endDate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(beginDate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(endDate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 计算两个日期间隔的天数
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return
	 */
	public static int daysBetween(String beginDate, String endDate) {
		SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.YMD);
		int daysBetween = 0;
		try {
			Date dateBefore = sdf.parse(beginDate);
			Date dateAfter = sdf.parse(endDate);
			daysBetween = daysBetween(dateBefore, dateAfter);
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		return daysBetween;
	}
	
	/**
	 * 计算两个日期间隔的分钟数
	 * 
	 * @param smdate
	 * @param bdate
	 * @return
	 */
	public static int minutesBetween(String before, String after) {
		Date dateBefore = DateUtil.StringToDate(before, ConstantDefine.YMD_HMS);
		Date dateAfter = DateUtil.StringToDate(after, ConstantDefine.YMD_HMS);
		long time1 =dateBefore.getTime();
		long time2 = dateAfter.getTime();
		long timesBetween = (time2 - time1)/1000/60;
		return Integer.parseInt(String.valueOf(timesBetween));
	}

	/**
	 * 判断某一天是星期几
	 * 
	 * @param dt
	 * @return
	 */
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 判断某一天是星期几
	 * 
	 * @param strDate
	 * @return
	 */
	public static String getWeekOfDate(String strDate) {
		Date dt = StringToDate(strDate, ConstantDefine.YMD);
		return getWeekOfDate(dt);
	}

	/**
	 * 判断一个时间是否为下午
	 * 
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static boolean isAfternoon(String strDate) {
		boolean flag = false;
		String defaultTime = "12:00";
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		Date dat;
		Date datDefault;
		try {
			dat = sdf.parse(strDate);
			datDefault = sdf.parse(defaultTime);
			if (dat.getTime() > datDefault.getTime()) {
				flag = true;
			}
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		return flag;
	}
	
	/**
	 * 将字符串转化成日期格式：20161005 ---> 2016-10-05
	 * 
	 * @param str
	 * @return
	 */
	public static String formatStringToYmd(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.YMD);
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
		String result = null;
		try {
			result = sdf.format(sdf2.parse(str));
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		return result;
	}

	/**
	 * 求两个时间相差的小时数
	 * 
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static int hoursBetween(String time1, String time2) {
		SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.HMS);
		long hours = 0;
		try {
			Date date1 = sdf.parse(time1);
			Date date2 = sdf.parse(time2);
			long millis = date2.getTime() - date1.getTime();
			long minutes = millis / (1000 * 60);
			if (minutes % 60 == 0) {
				hours = minutes / 60;
			} else {
				long hourRemain = minutes % 60;
				hours = (minutes - hourRemain) / 60 + 1;
			}
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		return (int) hours;
	}

	/**
	 * 获取年月日：2016-10-05 11:27:25.0 ---> 2016-10-05
	 * 
	 * @param strDate
	 * @return
	 */
	public static String getYMDfromDate(String strDate) {
		String result = strDate.substring(0, 10);
		return result;
	}

	/**
	 * 获取上个月或者这个月的第一天
	 * 
	 * @return
	 */
	public static String getFirstDay(int state) {
		Calendar calendar = Calendar.getInstance();
		if (state == 1) {
			calendar.add(Calendar.MONTH, -1);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
		} else if (state == 0) {
			calendar.set(Calendar.DAY_OF_MONTH, 1);
		}
		SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.YMD);
		return sdf.format(calendar.getTime());
	}

	/**
	 * 将字符串转化为时间 HH:mm:ss
	 * 
	 * @param times
	 * @return
	 */
	public static Date formateStringToTime(String times) {
		Date myTime = null;
		SimpleDateFormat sdf = new SimpleDateFormat(ConstantDefine.HMS);
		if (times != null) {
			try {
				myTime = sdf.parse(times);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return myTime;
	}

	public static void main(String[] args) throws ParseException {
		System.out.println(formateStringToTime("01:01:01"));

	
	}
	
	/**
	 * 比较传入的时间格式字符串与当前时间
	 * 大于当前日期     = 1,
	 * 小于当前日期     = -1,
	 * 等于当前日期     = 0
	 * @author leixin
	 * @createTime 下午12:46:26
	 * @param strDateTime 传入的时间字符串
	 * @return
	 * @throws StringIndexOutOfBoundsException
	 */
	public static int compareWithNow(String strDateTime) throws StringIndexOutOfBoundsException{
		String now = getSystemTimeStr();
		//截取年份
		String sysYear = now.substring(0,4);
		String year = strDateTime.substring(0,4);
		//截取月份
		String sysMonth = now.substring(5,7);
		String month = strDateTime.substring(5,7);
		//截取日
		String sysDay = now.substring(8,10);
		String day = now.substring(8,10);
		//判断年份
		if (Integer.parseInt(sysYear) > Integer.parseInt(year)) {
			return -1;
		}else if (Integer.parseInt(sysYear) < Integer.parseInt(year)) {
			return 1;
		}else {
			//判断月份
			if (Integer.parseInt(sysMonth) > Integer.parseInt(month)) {
				return -1;
			}else if (Integer.parseInt(sysMonth) < Integer.parseInt(month)) {
				return 1;
			}else {
				//判断日期
				if (Integer.parseInt(sysDay) > Integer.parseInt(day)) {
					return -1;
				}else if (Integer.parseInt(sysDay) < Integer.parseInt(day)) {
					return 1;
				}
			}
		}
		return 0;
	}
}
